﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security;
using BLL;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

public partial class FileUploadResize : System.Web.UI.UserControl
{
    public string FolderUpload { get; set; }

    public object ObjectControl { get; set; }

    public int width { get; set; }

    public int height { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.Page.User.Identity.IsAuthenticated)
            throw new SecurityException("Bạn không có quyền upload file.");

         lblFeedbackKO.Visible = false;
         lblFeedbackOK.Visible = false;
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (filUpload.PostedFile != null && filUpload.PostedFile.ContentLength > 0)
        {
            try
            {
                string url = this.Page.Request.ApplicationPath;
                if (!url.EndsWith("/"))
                    url=url + "/";

                string dirUrl = url+GetFolder(FolderUpload);
                string dirPath = Server.MapPath(dirUrl);
                if (!Directory.Exists(dirPath))
                    Directory.CreateDirectory(dirPath);
                string fileUrl = dirUrl + "/" + Path.GetFileName(filUpload.PostedFile.FileName);

                //Upload file len host
                //filUpload.PostedFile.SaveAs(Server.MapPath(fileUrl));

                System.Drawing.Image imageFull = System.Drawing.Image.FromStream(filUpload.PostedFile.InputStream);

                //thay doi width vao day de gioi han kich thuoc
                System.Drawing.Image image = new Bitmap(width, height, imageFull.PixelFormat);
                
                Graphics graphic = Graphics.FromImage(image);
                graphic.CompositingQuality = CompositingQuality.HighQuality;
                graphic.SmoothingMode = SmoothingMode.HighQuality;
                graphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                Rectangle rectangle = new Rectangle(0, 0, width, height);
                graphic.DrawImage(imageFull, rectangle);

                image.Save(Server.MapPath(fileUrl), ImageFormat.Png);
                imageFull.Dispose();
                //Gán đường dẫn Upload vào controls.

                if((ObjectControl!=null) &&(ObjectControl is TextBox))
                {
                    TextBox txtImageUrl = (TextBox)ObjectControl;
                    txtImageUrl.Text = fileUrl;
                }

                lblFeedbackOK.Visible = true;
                lblFeedbackOK.Text = "Upload file thành công : " + fileUrl;

            }
            catch (Exception ex)
            {
                lblFeedbackKO.Visible = true;
                lblFeedbackKO.Text = ex.Message;
            }
        }
    }

    string GetFolder(string folder)
    {
        string tr = "";
        switch (folder)
        {
            case "Departments":
                tr = "Upload/Departments";
                break;
            case "Products":
                tr = "Upload/Products";
                break;
            case "Categories":
                tr = "Upload/Categories";
                break;
            case "Articles":
                tr = "Upload/Articles";
                break;
            case "Partners":
                tr = "Upload/Partners";
                break;
            case "Intro":
                tr = "Upload/Intro";
                break;
            case "Thuvienhinhanh":
                tr = "Upload/Thuvienhinhanh";
                break;
            default:
                tr = "Upload";
                break;
        }
        return tr;
    }
}
